package com.ly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.entity.Team;
import com.ly.entity.User;
import com.ly.pojo.dto.TeamDTO;
import com.ly.pojo.vo.TeamVO;
import com.ly.service.TeamService;
import com.ly.service.UserService;
import com.ly.utils.ExcludeToken;
import com.ly.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author : 黑渊白花
 * @ClassName TeamController
 * @date : 2025/2/10 21:31
 * @Description
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/team")
@Slf4j
public class TeamController {
    @Autowired
    private TeamService teamService;
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    @ExcludeToken
    public Result list(){
        List<Team> teams = teamService.list();
        return Result.success(teams);
    }

    @GetMapping("/passList")
    @ExcludeToken
    public Result passList(){
        List<Team> teams = teamService.passList();
        return Result.success(teams);
    }

    @GetMapping("/pageAudit")
    public Object pageAudit( TeamDTO teamDTO){
        log.info("{}",teamDTO);
        IPage<TeamVO> auditPage = teamService.getAuditPage(teamDTO);
        return Result.success(auditPage);
    }

    // 管理员查询所有已审核的社团（status==2）
    @GetMapping("/Allpage")
    public Object Allpage( TeamDTO teamDTO){
        log.info("{}",teamDTO);
        IPage<TeamVO> allPage = teamService.getAllPage(teamDTO);
        return Result.success(allPage);
    }

    //该用户是社长添加失败，管理员添加的时候不用审核直接status=1
    @PostMapping("/save")
    public Result save(@RequestBody Team team){
        log.info("{}",team);
        User byName = userService.getByName(team.getManager());
        if(byName==null) {
            return Result.warn("用户不存在，添加失败");
        }
        userService.updateById(byName);
        team.setStatus(2);
        teamService.save(team);
        return Result.success();
    }

    @PostMapping("/createTeam")
    public Result createTeam(@RequestBody TeamVO teamVO){
        return teamService.createTeam(teamVO);
    }


    @PutMapping("/update")
    public Result update(@RequestBody Team team){
        teamService.updateById(team);
        return Result.success();
    }

    @PutMapping("/auditTeam")
    public Result auditTeam(@RequestBody Team team){
        teamService.auditTeam(team);
        return Result.success();
    }

    //如果能删除的化，把该社长变为学生type=2
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        Team byId = teamService.getById(id);
        Boolean delete = teamService.delete(byId);
        if(delete){
            return Result.success();
        }
        return Result.warn("社团还有成员，无法移除");
    }

    //通过社团id得到社团信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        TeamVO teamById = teamService.getTeamById(id);
        return Result.success(teamById);
    }


    //通过社长姓名得到社团信息
    @GetMapping("/getByManager/{name}")
    public Result getByManager(@PathVariable String name){
        Team team = teamService.getByManager(name);
        return Result.success(team);
    }

    //社团数量
    @GetMapping("/getTeamNum")
    @ExcludeToken
    public Result getTeamNum(){
        long count = teamService.count();
        return Result.success(count);
    }

    //得到社团名和该社团对应的成员数量最多八个
    @GetMapping("getTeamAndEnterNum")
    @ExcludeToken
    public Result getTeamAndEnterNum(){
        List<Map<String, Object>> teamAndEnterNum = teamService.getTeamAndEnterNum();
        return Result.success(teamAndEnterNum);
    }

    //
    @GetMapping("/searchTByType/{typeName}")
    public Result searchTByType(@PathVariable String typeName){
        List<TeamVO> teamVOS = teamService.searchTByType(typeName);
        return Result.success(teamVOS);
    }

    @GetMapping("/getTeamsByStuId/{stuId}")
    public Result getTeamsByStuId(@PathVariable Integer stuId){
        List<TeamVO> teamVOS = teamService.getTeamsByStuId(stuId);
        return Result.success(teamVOS);
    }

}
